package com.ivr.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import com.ivr.plat.dao.IFilterDao;
import com.ivr.plat.vo.FilterVo;
/**
 * 过滤信息数据实现层
 * @author liugeng
 *
 */
@Repository("filterDao")
public class FilterDaoImpl implements IFilterDao {
	private static Logger logger = Logger.getLogger(FilterDaoImpl.class);
	
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	/**获得过滤列表*/
	@Override
	public List<FilterVo> getFilterVoList() {
		String sql = "select filter.id, filter.num, filter.sp_number, filter.province, filter.start_time, filter.end_time, filter.thres_hold, ifnull(count_group.bill_count, 0) thres_index from " +
				"( SELECT bill.sp_number, COUNT(bill.mobile) bill_count FROM tb_ivr_user_bill as bill WHERE bill.add_date BETWEEN CURDATE() AND NOW() GROUP BY bill.sp_number ) as count_group " +
				"right join tb_ivr_filter as filter on filter.sp_number = count_group.sp_number " + 
				"where filter.status = 1 order by id";
		logger.debug(String.format("获得过滤列表[%s]", sql));
		List<FilterVo> filterVoList = jdbcTemplate.query(sql, rowMapper);
		if(filterVoList != null && filterVoList.size() > 0){
			return filterVoList;
		}
		return null;
	}
	
	private ParameterizedRowMapper<FilterVo> rowMapper = new ParameterizedRowMapper<FilterVo>() {
		@Override
		public FilterVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			FilterVo filterVo = new FilterVo();
			filterVo.setId(rs.getInt("id"));
			filterVo.setNum(rs.getInt("num"));
			filterVo.setThres_index(rs.getInt("thres_index"));
			filterVo.setSp_number(rs.getString("sp_number"));
			filterVo.setProvince(rs.getString("province"));
			filterVo.setStart_time(rs.getString("start_time"));
			filterVo.setEnd_time(rs.getString("end_time"));
			String tr_thres_hold = rs.getString("thres_hold");
			if((tr_thres_hold == null) || (filterVo.getNum() != tr_thres_hold.split(",").length)) {
				createThres_hold(filterVo);
			}
			else {
				String[] temp_thres = tr_thres_hold.split(",");
				List<Integer> thres_list = new ArrayList<Integer>();
				for(int i = 0; i < temp_thres.length; i++) {
					thres_list.add(Integer.parseInt(temp_thres[i]));
				}
				filterVo.setThres_hold(thres_list);
			}
			return filterVo;
		}
	};
	
	private int createThres_hold(FilterVo configFilterVo) {
		
		List<Integer> thres_hold = configFilterVo.getThres_hold();
		thres_hold.clear();
		
		if(configFilterVo.getNum() == 0)
			return 0;
		
		for(int i = 0; i < configFilterVo.getNum() ; i++) {
			int random = (int) (1+Math.random() * 100);
			logger.info(String.format("长号码[%s],随机数量[%d]，产生随机数序列号为[%d]", configFilterVo.getSp_number(), configFilterVo.getNum(), random));
			thres_hold.add(random);
		}
		
		configFilterVo.setThres_index(0);
		
		return thres_hold.size();
	}
}
